using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Diagnostics;using System.Drawing;using System.Linq;using System.Management;using System.Net;using System.Net.NetworkInformation;using System.Net.Sockets;using System.Text;using System.Text.RegularExpressions;using System.Threading;using System.Threading.Tasks;using System.Windows.Forms;namespace IP{ public partial class IP变更工具 : Form { List<NetworkAdapter> networkAdapter = new List<NetworkAdapter>(); public IP变更工具() { InitializeComponent(); } /// <summary> /// 获取所有适配器类型,适配器被禁用则不能获取到 /// </summary> /// <returns></returns> public void GetAllNetworkAdapters() //如果适配器被禁用则不能获取到 { NetworkInterface[] adapters = NetworkInterface.GetAllNetworkInterfaces(); foreach (NetworkInterface adapter in adapters) { if (adapter.OperationalStatus.ToString() == "Up") { if (adapter.NetworkInterfaceType == NetworkInterfaceType.Ethernet || adapter.NetworkInterfaceType == NetworkInterfaceType.Wireless80211) { if (adapter.Name.ToString().Contains("VMware") == false) { IPInterfaceProperties ips = adapter.GetIPProperties(); string description = adapter.Name;//描述 string networkInterfaceType = adapter.NetworkInterfaceType.ToString(); string speed = adapter.Speed / 1000 / 1000 "MB"; PhysicalAddress macAddress = adapter.GetPhysicalAddress(); //物理地址集合 string networkInterfaceID = adapter.Id;//网络适配器标识符 GatewayIPAddressInformationCollection getwaryes = ips.GatewayAddresses; //网关地址集合 UnicastIPAddressInformationCollection ipAddresses = ips.UnicastAddresses; //IP地址集合 IPAddressCollection dhcpServerAddresses = ips.DhcpServerAddresses;//DHCP地址集合 bool IsDhcpEnabled = ips.GetIPv4Properties() == null ? false : ips.GetIPv4Properties().IsDhcpEnabled; //是否启用DHCP服务 IPInterfaceProperties adapterProperties = adapter.GetIPProperties();//获取IPInterfaceProperties实例IPAddressCollection dnsAddresses = adapterProperties.DnsAddresses; comboBox1.Items.Add(description); // MessageBox.Show(adapter.OperationalStatus.ToString()); networkAdapter.Add(new NetworkAdapter(description, networkInterfaceType, speed, macAddress, networkInterfaceID, getwaryes, ipAddresses, dhcpServerAddresses, IsDhcpEnabled, dnsAddresses)); } } } } } /// <summary> /// 设置IP地址 /// </summary> /// <param name="ip"></param> /// <param name="submask"></param> /// <param name="getway"></param> /// <param name="dns"></param> private bool SetIPAddress(string[] ip, string[] submask, string[] getway, string[] dns) { string[] sArray = ip[0].Split('.'); if (Convert.ToInt32(sArray[3]) > 99) { ShowMessage("当前IP已经满足软电话IP需求无需变更。"); return true; } Random rd = new Random(); int i = rd.Next(140) 100; ip[0] = sArray[0] "." sArray[1] "." sArray[2] "." i.ToString(); ManagementClass wmi = new ManagementClass("Win32_NetworkAdapterConfiguration"); ManagementObjectCollection moc = wmi.GetInstances(); ManagementBaseObject inPar = null; ManagementBaseObject outPar = null; string str = ""; foreach (ManagementObject mo in moc) { if (!(bool)mo["IPEnabled"]) continue; if (TB_ID.Text == mo["SettingID"].ToString()) { if (ip != null && submask != null) { ShowMessage("开始设置IP"); string caption = mo["Caption"].ToString(); //描述 inPar = mo.GetMethodParameters("EnableStatic"); inPar["IPAddress"] = ip; inPar["SubnetMask"] = submask; outPar = mo.InvokeMethod("EnableStatic", inPar, null); str = outPar["returnvalue"].ToString(); // return (str == "0" || str == "1") ? true : false; if (str == "0") { ShowMessage("IP设置成功"); } else { return false; } //获取操作设置IP的返回值, 可根据返回值去确认IP是否设置成功。 0或1表示成功 } if (getway != null) { ShowMessage("开始设置网关"); inPar = mo.GetMethodParameters("SetGateways"); inPar["DefaultIPGateway"] = getway; outPar = mo.InvokeMethod("SetGateways", inPar, null); str = outPar["returnvalue"].ToString(); // return (str == "0" || str == "1") ? true : false; if (str == "0") { ShowMessage("网关设置成功"); } else { return false; } } if (dns != null) { ShowMessage("开始设置DNS"); inPar = mo.GetMethodParameters("SetDNSServerSearchOrder"); inPar["DNSServerSearchOrder"] = dns; outPar = mo.InvokeMethod("SetDNSServerSearchOrder", inPar, null); str = outPar["returnvalue"].ToString(); if (str == "0") { ShowMessage("DNS设置成功"); } else { return false; } //return (str == "0" || str == "1") ? true : false; } return true; } } return false; } public void ShowMessage(string msg) { lsvReceviedMsg.Invoke(new Action(() => { lsvReceviedMsg.Items.Insert(0, DateTime.Now.ToString("yyyy-mm-dd hh:mm:ss.fffffff") " " msg); })); } private void Form1_Load(object sender, EventArgs e) { GetAllNetworkAdapters(); comboBox1.SelectedIndex = 0; ShowIP(); // GetIP(comboBox1.SelectedItem.ToString()); } public void ShowIP() { foreach (NetworkAdapter nr in networkAdapter) { if (comboBox1.SelectedItem.ToString() == nr.Description) { TB_MAC.Text = nr.MacAddress.ToString(); TB_IP.Text = nr.IPAddresses.ToString(); TB_ID.Text = nr.NetworkInterfaceID; TB_DHCP.Text = nr.IsDhcpEnabled.ToString(); GatewayIPAddressInformationCollection gateways =nr.Getwaryes; foreach (UnicastIPAddressInformation ipadd in nr.IPAddresses) { //InterNetwork IPV4地址 InterNetworkV6 IPV6地址 //Max MAX 位址 if (ipadd.Address.AddressFamily == AddressFamily.InterNetwork) { TB_IP.Text = ipadd.Address.ToString();//获取ip TB_Mark.Text = ipadd.IPv4Mask.ToString(); // TB_NetType.Text = adapter.NetworkInterfaceType.ToString(); // TB_MAC.Text = adapter.GetPhysicalAddress().ToString(); string strGateway = ""; foreach (var gateWay in gateways) { //如果能够Ping通网关 if (IsPingIP(gateWay.Address.ToString())) { //得到网关地址 strGateway = gateWay.Address.ToString(); //跳出循环 break; } } TB_GetWay.Text = strGateway; IPAddressCollection dns = nr.DnsAddresses; TB_DNS1.Text = dns[0].ToString(); TB_DNS2.Text = dns[1].ToString(); break; } //判断是否为ipv4 } break; } } } // <summary> /// 尝试Ping指定IP是否能够Ping通 /// </summary> /// <param name="strIP">指定IP</param> /// <returns>true 是 false 否</returns> public static bool IsPingIP(string strIP) { try { //创建Ping对象 Ping ping = new Ping(); //接受Ping返回值 PingReply reply = ping.Send(strIP, 1000); //Ping通 return true; } catch { //Ping失败 return false; } } /// <summary> /// 若当前的IP不符合预设段位则进行变更 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void button2_Click(object sender, EventArgs e) { Thread thread = new Thread(new ThreadStart(Ipset)); thread.Start(); } public void Ipset() { comboBox1.Invoke(new Action(() => { button2.Enabled = false; button1.Enabled = false; })); string[] submask = { TB_Mark.Text }; string[] ip = { TB_IP.Text }; string[] getway = { TB_GetWay.Text }; string[] dns = { TB_DNS1.Text, TB_DNS2.Text }; bool se = SetIPAddress(ip, submask, getway, dns); if (se == true) { ShowMessage("整体设置成功"); } else { ShowMessage("设置失败,开始还原成DHCP自动获取"); EnableDHCP(); } comboBox1.Invoke(new Action(() => { comboBox1.Items.Clear(); GetAllNetworkAdapters(); comboBox1.SelectedIndex = 0; ShowIP(); button2.Enabled = true; button1.Enabled = true; })); } private void button1_Click(object sender, EventArgs e) { button2.Enabled = false; button1.Enabled = false; if (TB_DHCP.Text == "True") { ShowMessage("DHCP已启用,无需设置"); button2.Enabled = true; button1.Enabled = true; return; } EnableDHCP(); button2.Enabled = true; button1.Enabled = true; // TB_message.Text = "DHCP已开启"; } /// <summary> /// 启用DHCP服务 /// </summary> public void EnableDHCP() { ManagementClass wmi = new ManagementClass("Win32_NetworkAdapterConfiguration"); ManagementObjectCollection moc = wmi.GetInstances(); foreach (ManagementObject mo in moc) { if (!(bool)mo["IPEnabled"]) continue; if (mo["SettingID"].ToString() == TB_ID.Text) //网卡接口标识是否相等, 相当只设置指定适配器IP地址 { mo.InvokeMethod("SetDNSServerSearchOrder", null); mo.InvokeMethod("EnableDHCP", null); } } ShowMessage("DHCP还原完毕"); } private void comboBox1_TextChanged(object sender, EventArgs e) { ShowIP(); } }}
下载C# 获取所有网卡IP信息用户还喜欢
- 18480 文章数
- 500万+ 热度
作者专栏
编辑推荐
- 淡抹u2引擎,修复内容较多,物有所值
- 界域传说·经典巨作=传世单机(一键安装)
- 丸子版本(175个传世版本大集合)
- GS版本:神话公益服务端+客户端
- 图片放大工具(放大图片不模糊)
- 剪映无限制VIP版
- 传奇世界客户端下载器,史上最全传世客户端
- 传世GS20220920商业引擎注册+登录配置器 解压密码是1
- U2官方排行榜游戏网关 支持元神,支持传家宝
- GS开战传世客户端+服务端
- (淡漠夕阳)u2引擎合区工具
- 传世GS引擎消除“你的游戏客户端版本号过旧,请及时更新”提示
- 传世一机多区双线路配置器--免密码版本
- 传世凤凰登陆器劫持修复软件
- SQLite3 for Navicat
- 传奇世界npc对话框编辑工具
- 传世GS落霞铭文服务器端
- gs_20210409引擎包+注册机(无限制)
- 传奇世界NPC对话封包查看器[支持时长版和极速版]
- 彩虹引擎传世脚本编辑工具1.7版来了,支持函数脚本翻译
评论